package com.yxhl.zoume.common.ui.custom;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.DrawableRes;
import android.support.v7.widget.AppCompatCheckBox;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import com.yxhl.zoume.R;

/**
 * Created by sugarya on 16/4/27.
 * 用来显示星星评分,提供只读/可选中两种方式
 */
public class CheckBoxLayout extends LinearLayout {

    private static final String TAG = "CheckBoxLayout";

    private boolean mCheckEnable = true;
    private int mBoxCount = 5;
    private double mCheckedCount = 0;
    private
    @DrawableRes
    int mResId;
    private int itemMarginRight;

    private boolean needRun = false;

    private onSelectedListener onSelectedListener;


    public CheckBoxLayout(Context context) {
        super(context);
        initializeLayout(context);
    }

    public CheckBoxLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.checkBoxLayout);
        mCheckEnable = typedArray.getBoolean(R.styleable.checkBoxLayout_checkEnable, true);
        mBoxCount = typedArray.getInteger(R.styleable.checkBoxLayout_boxCount, 5);
        mResId = typedArray.getResourceId(R.styleable.checkBoxLayout_boxSrc, R.drawable.star_selector);
        mCheckedCount = typedArray.getInt(R.styleable.checkBoxLayout_checkedCount, 0);
        itemMarginRight = (int) typedArray.getDimension(R.styleable.checkBoxLayout_item_margin_right, 1);
        typedArray.recycle();
        initializeLayout(getContext());
    }

    private void initializeLayout(Context context) {
        //Log.e(TAG, "initializeLayout");
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(0, 0, itemMarginRight, 0);
        for (int i = 0; i < mBoxCount; i++) {
            final int position = i;
            AppCompatCheckBox checkBox = new AppCompatCheckBox(context);
            checkBox.setButtonDrawable(mResId);
            checkBox.setLayoutParams(layoutParams);
            if (i < mCheckedCount) {
                checkBox.setChecked(true);
            }

            checkBox.setEnabled(mCheckEnable);
            checkBox.setTag(i);
            checkBox.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    int order = (Integer) v.getTag();
                    initializeCheckedStatus(order);
                    mCheckedCount = position + 1;
                    if (v != null && onSelectedListener != null) {
                        onSelectedListener.onSelected(v, position);
                    }

                }
            });
            if (getChildCount() < mBoxCount) {
                this.addView(checkBox);
            }
        }

    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        //Log.e(TAG, "onLayout: ");
        if (needRun) {
            reDraw();
            needRun = false;
        }

    }

    private void refreshCheckedStatus(double order) {
        //Log.e(TAG, "refreshCheckedStatus:order="+order);
        //清空选中状态
        for (int i = 0; i < mBoxCount; i++) {
            CompoundButton childAt = (CompoundButton) getChildAt(i);
            if (childAt != null) {
                childAt.setChecked(false);
            }
        }
        for (int i = 0; i < mBoxCount; i++) {
            CompoundButton childAt = (CompoundButton) getChildAt(i);
            if (childAt != null) {
                if (i < order) {
                    childAt.setChecked(true);
                }
            }
        }
    }

    private void refreshEnable(boolean enable) {
        for (int i = 0; i < mBoxCount; i++) {
            CompoundButton childAt = (CompoundButton) getChildAt(i);
            if (childAt != null) {
                childAt.setEnabled(enable);
            }
        }
    }

    private void refreshResId(@DrawableRes int resId) {
        for (int i = 0; i < mBoxCount; i++) {
            CompoundButton childAt = (CompoundButton) getChildAt(i);
            if (childAt != null) {
                childAt.setButtonDrawable(resId);
            }
        }
    }

    private void initializeCheckedStatus(int order) {
        //Log.e(TAG, "refreshCheckedStatus:order="+order);
        //清空选中状态
        for (int i = 0; i < mBoxCount; i++) {
            CompoundButton childAt = (CompoundButton) getChildAt(i);
            if (childAt != null) {
                childAt.setChecked(false);
            }
        }
        for (int i = 0; i < mBoxCount; i++) {
            CompoundButton childAt = (CompoundButton) getChildAt(i);
            if (childAt != null) {
                if (i <= order) {
                    childAt.setChecked(true);
                }
            }
        }
    }


    /**
     * 刷新视图
     */
    private void reDraw() {
        //Log.e(TAG, "reDraw");
        this.removeAllViews();
        initializeLayout(this.getContext());
    }

    public boolean isCheckEnable() {
        return mCheckEnable;
    }

    public void setCheckEnable(boolean checkEnable) {
        this.mCheckEnable = checkEnable;
        refreshEnable(checkEnable);
    }

    public int getBoxCount() {
        return mBoxCount;
    }

    public void setBoxCount(int boxCount) {
        //Log.e(TAG, "setBoxCount: boxCount="+boxCount);
        needRun = true;
        this.mBoxCount = boxCount;
    }

    public double getCheckedCount() {
        return mCheckedCount;
    }

    public void setCheckedCount(double checkedCount) {
        //Log.e(TAG, "setCheckedCount: checkedCount="+checkedCount);
        if (checkedCount <= mBoxCount) {
            this.mCheckedCount = checkedCount;
            refreshCheckedStatus(checkedCount);
        } else {
            throw new IllegalStateException("the count of checked box cannot more than capacity itself");
        }
    }

    public int getResId() {
        return mResId;
    }

    public void setResId(int boxResId) {
        this.mResId = boxResId;
        refreshResId(boxResId);
    }

    public void setOnSelectedListener(onSelectedListener onSelectedListener) {
        this.onSelectedListener = onSelectedListener;
    }


    public interface onSelectedListener {
        void onSelected(View view, int position);
    }

}
